<template>
  <div class="strategy-container">
        <!-- 头部区域 - 参考B站风格 -->
        <header class="strategy-header">
            <div class="logo-area">
                <img src="@/assets/logo.png" alt="山行者Logo" class="logo" />
                <h1 class="site-title">山行者笔记</h1>
            </div>

            <nav class="main-nav">
                <ul>
                    <li><a href="#" class="active">发布笔记</a></li>
                </ul>
            </nav>
      <div class="user-actions">
        <el-dropdown trigger="click">
          <span class="el-dropdown-link user-profile">
            <el-avatar :size="32" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
            <span class="username">用户名</span>
            <el-icon><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>设置</el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <main class="strategy-content">
      <el-card class="note-form-card">
        <template #header>
          <div class="card-header"><h2>编辑笔记</h2></div>
        </template>

        <el-form :model="note" label-width="80px" ref="formRef">
          <el-form-item label="标题" prop="title">
            <el-input v-model="note.title" placeholder="请输入标题" />
          </el-form-item>

          <el-form-item label="分类" prop="category">
            <el-select v-model="note.category" placeholder="请选择分类">
              <el-option label="旅游攻略" value="travel" />
              <el-option label="景点介绍" value="scenic" />
              <el-option label="酒店推荐" value="hotel" />
              <el-option label="跟团游" value="grouptour" />
            </el-select>
          </el-form-item>

          <el-form-item label="内容" prop="content">
            <div class="quill-editor">
              <div ref="quillEditor" class="editor"></div>
            </div>
          </el-form-item>

          <el-form-item label="位置" prop="location">
            <el-select v-model="note.location" placeholder="请选择地点">
              <el-option label="北京" value="北京" />
              <el-option label="上海" value="上海" />
              <el-option label="广州" value="广州" />
              <el-option label="成都" value="成都" />
              <el-option label="杭州" value="杭州" />
              <el-option label="西安" value="西安" />
              <el-option label="张家界" value="张家界" />
              <el-option label="九寨沟" value="九寨沟" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm">保存修改</el-button>
            <el-button @click="goBack" style="margin-right: 10px;">返回</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'
import Quill from 'quill'
import 'quill/dist/quill.snow.css'

const route = useRoute()
const router = useRouter()
const formRef = ref(null)
const quillEditor = ref(null)
let quill = null

const note = ref({
  title: '',
  category: '',
  content: '',
  visibility: '',
  location: '',
  coverImage: '',
})

const fetchNote = async () => {
  try {
    const res = await axios.get(`http://localhost:8080/api/note/${route.params.id}`)
    note.value = res.data
    nextTick(() => {
      if (quill && note.value.content) {
        quill.root.innerHTML = note.value.content
      }
    })
  } catch (error) {
    ElMessage.error('获取笔记失败')
  }
}

const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        note.value.content = quill.root.innerHTML
        await axios.put(`http://localhost:8080/api/note/${route.params.id}`, note.value)
        ElMessage.success('笔记修改成功')
        router.push('/userframe/strategy/published')
      } catch (error) {
        ElMessage.error('修改失败')
      }
    }
  })
}

const goBack = () => {
  if (route.params.id) {
    router.push(`/note/detail/${route.params.id}`);
  } else {
    router.back(); // 如果没有 id，就回退一页
  }
};

const handleLogout = () => {
  ElMessage.success('已退出登录')
}

onMounted(() => {
  fetchNote()

  nextTick(() => {
    if (quillEditor.value) {
      quill = new Quill(quillEditor.value, {
        theme: 'snow',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            [{ direction: 'rtl' }],
            [{ size: ['small', false, 'large', 'huge'] }],
            [{ color: [] }, { background: [] }],
            [{ font: [] }],
            [{ align: [] }],
            ['link', 'image', 'video'],
            ['clean']
          ]
        },
        placeholder: '请输入笔记内容...'
      })

      quill.on('text-change', () => {
        note.value.content = quill.root.innerHTML
      })
    }
  })
})
</script>

<style scoped>
.strategy-container {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.strategy-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 12px 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.logo-area {
  display: flex;
  align-items: center;
}

.logo {
  height: 36px;
  margin-right: 12px;
}

.site-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.main-nav ul {
  list-style: none;
  display: flex;
  gap: 20px;
}

.main-nav a {
  color: #606266;
  text-decoration: none;
  font-size: 16px;
}

.main-nav a.active {
  color: #409EFF;
  font-weight: bold;
}

.user-actions {
  display: flex;
  align-items: center;
}

.user-profile {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 6px;
  font-size: 14px;
}

.strategy-content {
  max-width: 900px;
  margin: 40px auto;
  padding: 20px;
}

.note-form-card {
  border-radius: 8px;
}

.card-header {
  font-size: 20px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 12px;
}

.quill-editor {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  min-height: 300px;
  background-color: #ffffff;
}

.editor {
  height: 300px;
}
</style>
